package cn.iocoder.yudao.module.promotion.controller.admin.promotion;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.promotion.controller.admin.promotion.vo.*;
import cn.iocoder.yudao.module.promotion.dal.dataobject.promotion.PromotionDO;
import cn.iocoder.yudao.module.promotion.service.promotion.PromotionService;

@Tag(name = "管理后台 - 促销")
@RestController
@RequestMapping("/promotion/promotion")
@Validated
public class PromotionController {

    @Resource
    private PromotionService promotionService;

    @PostMapping("/create")
    @Operation(summary = "创建促销")
    @PreAuthorize("@ss.hasPermission('promotion:promotion:create')")
    public CommonResult<Long> createPromotion(@Valid @RequestBody PromotionSaveReqVO createReqVO) {
        return success(promotionService.createPromotion(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新促销")
    @PreAuthorize("@ss.hasPermission('promotion:promotion:update')")
    public CommonResult<Boolean> updatePromotion(@Valid @RequestBody PromotionSaveReqVO updateReqVO) {
        promotionService.updatePromotion(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除促销")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('promotion:promotion:delete')")
    public CommonResult<Boolean> deletePromotion(@RequestParam("id") Long id) {
        promotionService.deletePromotion(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得促销")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('promotion:promotion:query')")
    public CommonResult<PromotionRespVO> getPromotion(@RequestParam("id") Long id) {
        PromotionDO promotion = promotionService.getPromotion(id);
        return success(BeanUtils.toBean(promotion, PromotionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得促销分页")
    @PreAuthorize("@ss.hasPermission('promotion:promotion:query')")
    public CommonResult<PageResult<PromotionRespVO>> getPromotionPage(@Valid PromotionPageReqVO pageReqVO) {
        PageResult<PromotionDO> pageResult = promotionService.getPromotionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PromotionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出促销 Excel")
    @PreAuthorize("@ss.hasPermission('promotion:promotion:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPromotionExcel(@Valid PromotionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PromotionDO> list = promotionService.getPromotionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "促销.xls", "数据", PromotionRespVO.class,
                        BeanUtils.toBean(list, PromotionRespVO.class));
    }

}